package svg;

import java.awt.Color;
import java.net.URI;
import java.util.ArrayList;

public class Container extends GObject
{
	protected ArrayList<GObject> elements;
	
	public Container(){}
	
	/* Container
	 * x - left top corner coordinate x
	 * y - left top corner coordinate y
	 * width - container width
	 * height - container height
	 */
	public Container(int x, int y, int width, int height)
	{
		super(x, y, width, height);

		this.elements = new ArrayList<GObject>();
	}
	
	public void init(){}
	
	public void layout(int gap)
	{
		int numberOfTasks = elements.size();
		int taskCanvasHeight = (getHeight() - (numberOfTasks + 1) * gap) / numberOfTasks;
        int taskCanvasWidth = getWidth() - 2 * gap;
        int taskCanvasX = getX() + gap;
        
        int taskCanvasCurrentY = getY() + gap;
        int maxTitleWidth = 0;
        
        for(GObject e : elements)
        {
        	e.setX(taskCanvasX);
        	e.setY(taskCanvasCurrentY);
        	e.setWidth(taskCanvasWidth);
        	e.setHeight(taskCanvasHeight);
        	
        	if(e instanceof TaskCanvas)
        	{
        		TaskCanvas tc = (TaskCanvas) e;
        		if(maxTitleWidth < tc.getTitleWidth())
        		{
        			maxTitleWidth = tc.getTitleWidth();
        		}
        	}
        	
        	taskCanvasCurrentY += gap + taskCanvasHeight;
        }
        
        for(GObject e : elements)
        {
        	if(e instanceof TaskCanvas)
        	{
        		TaskCanvas tc = (TaskCanvas) e;
        		tc.setTitleWidth(maxTitleWidth);
        	}
        }
        
        for(GObject e : elements)
        {
        	e.init();
        }
	}
	
	@Override
	public void show(URI uri) 
	{
		JobViewer.addRectangle(uri, 
        		x, y, width, height,
        		true, Color.WHITE, 
        		true, Color.BLACK, 1);
		
		for(GObject element: elements)
		{
			element.show(uri);
		}
		
	}

	public ArrayList<GObject> getElements() {
		return elements;
	}

	public void setElements(ArrayList<GObject> elements) {
		this.elements = elements;
	}

}
